package com.software.cool.api.admin.v1.controller;
import com.software.cool.api.admin.v1.vo.AdminLoginUserVO;
import com.software.cool.api.admin.v1.vo.AdminV1Result;
import com.software.cool.common.CommonContant;
import com.software.cool.api.app.v1.vo.AppV1Result;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Slf4j
public class BaseController {

    @Resource
    private HttpServletRequest request;

    public String isAdminIndex(HttpServletRequest request,String page,boolean needLogin)
    {
        if(needLogin)
        {
            if(isAdminLogin(request))
            {
                if(page.equals("/page/login"))
                {
                    page="index";
                }
                return page;
            }else{
                return "page/login";
            }
        }else{
            return page;
        }
    }

    public boolean isAdminLogin(HttpServletRequest request)
    {
        boolean result=false;
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.ADMIN_LOGIN_SESSION);
        if(loginUser!=null)
        {
            result=true;
        }
        return result;
    }

    public AdminV1Result getAdmin(String token)
    {
        AdminV1Result result=new AdminV1Result();
        result.setCode(-1);
        result.setMsg("未登录");
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.ADMIN_LOGIN_SESSION);
        if(loginUser!=null && loginUser.getToken().equals(token))
        {
            result.setCode(0);
            result.setMsg("已登陆");
        }
        result.setData(loginUser);
        return result;
    }


    public boolean isAppLogin(HttpServletRequest request)
    {
        boolean result=false;
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.APP_LOGIN_SESSION);
        if(loginUser!=null)
        {
            result=true;
        }
        return result;
    }

    public AppV1Result getAppUser(String token)
    {
        AppV1Result result=new AppV1Result();
        result.setSuccess(false);
        result.setErrMsg("未登录");
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.APP_LOGIN_SESSION);
        if(loginUser!=null && loginUser.getToken().equals(token))
        {
            result.setSuccess(true);
            result.setErrMsg("已登陆");
        }
        result.setData(loginUser);
        return result;
    }

    /**
     * 从request中获得参数Map，并返回可读的Map
     *
     * @param request
     * @return
     */
    protected Map getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }

}
